<template>
  <div class="login-container">
    <el-row>
      <el-card class="paperitem" v-for="item in list"  style="float: left;width: 260px"  >
        <div :class="{'active':item.type == 1}"  >
          <span style="font-size: x-large">{{item.name}}</span>
          <el-button v-if="item.answered == false" @click="gotest(item.id)" style="float: right; padding: 3px 0" type="text">立即考试</el-button>
          <hr>
          <img class="paperimg" :src="'http://localhost:8080/downloadFile/'+ item.img"  />
          <div>
            <div class="info">
              开始时间：{{item.createtime}}
            </div>
            <div class="info">
              结束时间：{{item.endtime}}
            </div>
            <div class="info">
              总分：{{item.totalscore}}
            </div>
            <div class="info">
              测试类型：<span v-if="item.type==1"> 练习题</span> <span v-if="item.type==2"> 考试</span>
            </div>
            <div class="info">
              答题状态：<el-tag v-if="item.answered == true" >已作答</el-tag> <el-tag v-if="item.answered==false" type="success">未作答</el-tag>
            </div>
          </div>
        </div>
      </el-card>
    </el-row>
  </div>
</template>
<script>
import { login, logout,getMyPaper,getMyAnswerResultSimple } from "@/api/student";
import { getToken, setToken, removeToken } from "@/utils/auth";

export default {
  name: "login",
  data() {
    const validatename = (rule, value, callback) => {
      callback();
    };
    const validatePass = (rule, value, callback) => {
      if (value.length < 2) {
        callback(new Error("密码不能小于2位"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        name: "123",
        password: "123",
      },
      loginRules: {
        name: [{ required: true, trigger: "blur", validator: validatename }],
        password: [
          { required: true, trigger: "blur", validator: validatePass },
        ],
      },
      list:[],
      loading: false,
      pwdType: "password",
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    gotest(e){
      this.$router.push('/student/question?id='+ e)
      console.log(e)
    },
    fetchData(){
      getMyPaper({}).then(res=>{
        console.log(res)

        getMyAnswerResultSimple({}).then(res2=>{
          res.info.forEach(ele=>{

              if(res2.info.indexOf(ele.id) >0){
                ele.answered = true;
              }else
                ele.answered = false;
          })
          this.list = res.info

        })
      })
    },
    showPwd() {
      if (this.pwdType === "password") {
        this.pwdType = "";
      } else {
        this.pwdType = "password";
      }
    },

    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;

          login(this.loginForm.name, this.loginForm.password)
            .then((response) => {
              localStorage.setItem("logininfo", JSON.stringify(response));
              this.loading = false;
              this.$router.push({ path: "/student" });
            })
            .catch((error) => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #ffffff;
$dark_gray: #ffffff;
$light_gray: rgb(250, 250, 250);

  .paperitem{
    margin-top: 20px;
    margin-left: 10px;
  }
  .paperimg{
    width: 200px;
    height: 100px;
  }
  .info{
    font-size: small;
  }
.active{
}
</style>
